# -*- coding: utf-8 -*-
# @Author    : Tian Wencheng
# @CreateTime: 2021/1/12 15:47
# @FileName  : sql_operate.py.py
# @IDE       : PyCharm
# @Git       : https://gitee.com/dianetian/projects


from common.sql_operate import SqlOperate
from common.log import logger


class SqlProcess(SqlOperate):
    def __init__(self, db_ip: str = "127.0.0.1", db_name_case: str = "", db_username: str = "", db_password: str = "",
                 db_port: int = 3306):
        super().__init__(ip_addr=db_ip, db_name=db_name_case, username=db_username, password=db_password, port=db_port)

    def __del__(self):
        super().__del__()

    def select_test_case_num(self):
        return self.select_one("select count(1) from interface_case;")

    def update_common_response(self, table_name, response_content, response_duration, case_number):
        update_sql = f"update {table_name} set response_content='{response_content}', duration={response_duration} " \
                     f"where case_number='{case_number}';"
        logger.info(f"[{self.__class__.__name__}] update_sql={update_sql}")
        return self.operate_sql(update_sql)

    def update_return_code_compare_result(self, table_name, actual_return_code, case_number, return_code_compare_result):
        update_sql = f"update {table_name} set actual_return_code={actual_return_code}, " \
                     f"return_code_compare_result={return_code_compare_result} where case_number='{case_number}';"
        logger.info(f"[{self.__class__.__name__}] update_sql={update_sql}")
        return self.operate_sql(update_sql)

    def update_structure_complete_compare_result(self, table_name, case_number, structure_complete_compare_result):
        update_sql = f"update {table_name} set structure_complete_compare_result={structure_complete_compare_result} " \
                     f"where case_number='{case_number}';"
        logger.info(f"[{self.__class__.__name__}] update_sql: {update_sql}")
        return self.operate_sql(update_sql)

    def update_param_complete_compare_result(self, table_name, case_number, params, param_complete_compare_result):
        update_sql = f"update {table_name} set actual_params='{params}', param_complete_compare_result=" \
                     f"{param_complete_compare_result} where case_number='{case_number}';"
        logger.info(f"[{self.__class__.__name__}] update_sql: {update_sql}")
        return self.operate_sql(update_sql)

    def update_param_value_compare_result(self, table_name, case_number, actual_param_value, param_value_compare_result):
        update_sql = f"update {table_name} set actual_param_value='{actual_param_value}', param_value_compare_result=" \
                     f"{param_value_compare_result} where case_number='{case_number}';"
        logger.info(f"[{self.__class__.__name__}] update_sql: {update_sql}")
        return self.operate_sql(update_sql)

    def update_result_as_failure(self, table_name, case_number):
        update_sql = f"update {table_name} set return_code_compare_result = 0, structure_complete_compare_result = 0, " \
                     f"param_complete_compare_result = 0, param_value_compare_result = 0 where case_number = '{case_number}';"
        logger.info(f"[{self.__class__.__name__}] update sql: {update_sql}")
        return self.operate_sql(update_sql)


if __name__ == "__main__":
    print(SqlProcess().select_test_case_num())

